package com.miniot.base.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miniot.base.dto.BoundManageDTO;
import com.miniot.base.entity.BoundManage;
import com.miniot.base.entity.TypeOne;
import com.miniot.base.entity.WarehouseManage;
import com.miniot.base.service.BoundManageService;
import com.miniot.base.vo.BoundManageDataVO;
import com.miniot.base.vo.BoundManageQueryVO;
import com.miniot.base.vo.BoundManageVO;
import com.miniot.base.vo.InventoryListDataVO;
import com.miniot.business.base.BaseController;
import com.miniot.business.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 出入库管理表
 *
 * @author zzq
 * @date 2023-10-17 11:13:54
 */
@RestController
@AllArgsConstructor
@RequestMapping("/boundmanage" )
@Api(value = "出入库管理表(后台)", tags = "出入库管理表-后台接口")

public class BoundManageController extends BaseController {

    private final BoundManageService boundManageService;

    /**
    * 分页查询
    * @param currentPage 当前页数
    * @param pageSize 每页数据量
    * @param boundManage 查询条件
    * @return 查询结果
    */
    @GetMapping("/listPage")
    @ApiOperation(value = "库存变更查询", notes = "传入分页信息与查询条件")
    public R<Page<BoundManage>> listBoundManagePage(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询条件") BoundManageQueryVO boundManage) {
        Page<BoundManage> page = new Page<BoundManage>(currentPage, pageSize);
        return R.success(boundManageService.page(page, new LambdaQueryWrapper<BoundManage>()
                .eq(ObjectUtil.isNotEmpty(boundManage.getOperationTime()),BoundManage::getOperationTime,boundManage.getOperationTime())
                .like(ObjectUtil.isNotEmpty(boundManage.getWarehouseName()),BoundManage::getWarehouseName,boundManage.getWarehouseName())
                .orderByDesc(BoundManage::getCreateTime)),"分页信息查询成功");
    }


    /**
    * 通过id查询出入库管理表
    * @param id id
    * @return Result
    */
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查询出入库管理表", notes = "传入id")
    public R<BoundManageDTO> getBoundManageById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        return R.success(boundManageService.getBoundManageById(id));
    }

    /**
    * 新增出入库管理表
    * @param boundManage 出入库管理表
    * @return Result
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增出入库管理表", notes = "传入BoundManage数据")
    public R<Long> save(@RequestBody BoundManageVO boundManage) {
        return R.success(boundManageService.saveBoundManage(boundManage),"新增成功");
    }

    /**
    * 修改出入库管理表
    * @param boundManage 出入库管理表
    * @return Result
    */
    @PostMapping("/updateById")
    @ApiOperation(value = "修改出入库管理表", notes = "传入BoundManage数据")
    public R updateById(@RequestBody BoundManage boundManage) {
        Assert.isTrue(boundManageService.updateById(boundManage), "修改失败");
        return R.success(true,"修改成功");
    }

    /**
    * 通过id删除出入库管理表
    * @param id id
    * @return Result
    */
    @GetMapping("/deleteById")
    @ApiOperation(value = "通过id删除出入库管理表", notes = "传入id")
    public R deleteById(
            @ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        Assert.isTrue(boundManageService.removeById(id), "操作失败");
        return R.success(true,"删除成功");
    }

    /**
     * 查询入库管理数据列表
     */
    @GetMapping("/selectManagementData")
    @ApiOperation(value = "查询入库管理数据列表", notes = "")
    public R<Page<BoundManageDataVO>> selectManagementData(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询时间") @RequestParam("time") String time,
            @ApiParam(value = "仓库名称") @RequestParam("warehouseName") String warehouseName,
            @ApiParam(value = "订单号") @RequestParam("orderNum") String orderNum) {
        Page<BoundManage> page = new Page(currentPage, pageSize);
        Page<BoundManageDataVO> list = boundManageService.selectManagementData(page,time,warehouseName,orderNum);
        return R.success(list,"查询成功");
    }

    /**
     * 实时库存-数据列表
     */
    @GetMapping("/inventoryListData")
    @ApiOperation(value = "实时库存-数据列表", notes = "")
    public R<Page<InventoryListDataVO>> getInventoryListData(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "仓库名称") @RequestParam("warehouseName") String warehouseName) {
        Page<WarehouseManage> page = new Page(currentPage, pageSize);
        Page<InventoryListDataVO> list = boundManageService.getInventoryListData(page,warehouseName);
        return R.success(list,"查询成功");
    }
}